package com.pangda.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.common.utils.UserContext;
import com.pangda.pojo.DictType;
import com.pangda.mapper.DictTypeMapper;
import com.pangda.pojo.dto.DictPageParam;
import com.pangda.pojo.dto.DictTypeDto;
import com.pangda.pojo.vo.DictTypeVo;
import com.pangda.service.IDictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author Panda.No72
 * @since 2024-06-20
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements IDictTypeService {


    @Override
    public ResponseResult add(DictTypeDto dictTypeDto) {
        dictTypeDto.setCreateBy(UserContext.getUser());
        dictTypeDto.setDictName(dictTypeDto.getDictName().trim());
        dictTypeDto.setDictType(dictTypeDto.getDictType().trim());
        DictType dictType = BeanUtil.toBean(dictTypeDto, DictType.class);
        return ResponseResult.success(save(dictType));
    }

    @Override
    public PageResult<DictTypeVo> getList(DictPageParam param) {

        if (BeanUtil.isNotEmpty(param)){
            LambdaQueryWrapper<DictType> wrapper = new LambdaQueryWrapper<>();
            if (StrUtil.isNotBlank(param.getDictName())){
                wrapper.like(DictType::getDictName, param.getDictName());
            }
            if (StrUtil.isNotBlank(param.getDictType())){
                wrapper.like(DictType::getDictType, param.getDictType());
            }
            if (StrUtil.isNotBlank(param.getStatus())){
                wrapper.eq(DictType::getStatus, param.getStatus());
            }
            if (BeanUtil.isNotEmpty(param.getParams())){
                wrapper.ge(DictType::getCreateTime, param.getParams().get("beginTime"));
                wrapper.le(DictType::getCreateTime, param.getParams().get("endTime") + " 23:59:59");
            }
            Page<DictType> page = page(new Page<>(param.getPageNum(), param.getPageSize()),wrapper);
            return PageResult.success(page, DictTypeVo.class);
        } else {
            List<DictType> list = list();
            return PageResult.success(list, DictTypeVo.class);
        }
    }

    @Override
    public ResponseResult replace(DictTypeDto dictTypeDto, Integer dictId) {
        DictType dictType = BeanUtil.toBean(dictTypeDto, DictType.class);
        dictType.setDictId(dictId);
        return ResponseResult.success(updateById(dictType));
    }

    @Override
    public ResponseResult getInfo(Integer dictId) {
        DictType dictType = getById(dictId);
        DictTypeVo dictTypeVo = BeanUtil.toBean(dictType, DictTypeVo.class);
        return ResponseResult.success(dictTypeVo);
    }
}
